// The contents of this file are subject to the Mozilla Public License Version
// 1.1
//(the "License"); you may not use this file except in compliance with the
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
//
//Software distributed under the License is distributed on an "AS IS" basis,
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
//for the specific language governing rights and
//limitations under the License.
//
//The Original Code is "The Columba Project"
//
//The Initial Developers of the Original Code are Frederik Dietz and Timo
// Stich.
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
//
//All Rights Reserved.
package org.columba.mail.gui.message.action;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ButtonGroup;
import javax.swing.JRadioButtonMenuItem;

import org.columa.core.config.IDefaultItem;
import org.columba.api.gui.frame.IFrameMediator;
import org.columba.core.config.DefaultItem;
import org.columba.core.gui.menu.IMenu;
import org.columba.core.xml.XmlElement;
import org.columba.mail.config.MailConfig;

/**
 * Submenu containing three choices: Show default headers, show custom headers
 * and show all available headers.
 * 
 * @author fdietz
 */
public class HeadersMenu extends IMenu implements ActionListener, Observer {

	private XmlElement element;

	private JRadioButtonMenuItem defaultMenuItem;

	private JRadioButtonMenuItem customMenuItem;

	private JRadioButtonMenuItem allMenuItem;
	
	/**
	 * @param controller
	 * @param caption
	 */
	public HeadersMenu(IFrameMediator controller) {
		super(controller, "Show Headers","show_headers_menu");

		ButtonGroup group = new ButtonGroup();

		defaultMenuItem = new JRadioButtonMenuItem("Default");
		defaultMenuItem.setActionCommand("DEFAULT");
		defaultMenuItem.addActionListener(this);
		group.add(defaultMenuItem);

		add(defaultMenuItem);

		customMenuItem = new JRadioButtonMenuItem("Custom");
		customMenuItem.setActionCommand("CUSTOM");
		customMenuItem.addActionListener(this);
		group.add(customMenuItem);
		add(customMenuItem);

		allMenuItem = new JRadioButtonMenuItem("Compact");
		allMenuItem.setActionCommand("ALL");
		allMenuItem.addActionListener(this);
		group.add(allMenuItem);
		add(allMenuItem);

		element = MailConfig.getInstance().get("options").getElement(
				"/options/headerviewer");
		element.addObserver(this);

		update(element, null);
	}

	public void actionPerformed(ActionEvent e) {
		String action = e.getActionCommand();

		if (action.equals("DEFAULT")) {
			element.addAttribute("style", "0");

			new ViewMessageAction(getFrameMediator()).actionPerformed(null);
		} else if (action.equals("CUSTOM")) {
			element.addAttribute("style", "1");

			new ViewMessageAction(getFrameMediator()).actionPerformed(null);
		} else if (action.equals("ALL")) {
			element.addAttribute("style", "2");

			new ViewMessageAction(getFrameMediator()).actionPerformed(null);
		}
	}

	/**
	 * Method is called when configuration changes.
	 * 
	 * @param arg0
	 * @param arg1
	 */
	public void update(Observable arg0, Object arg1) {
		IDefaultItem item = new DefaultItem(element);
		int style = item.getIntegerWithDefault("style", 0);
		switch (style) {
		case 0:
			defaultMenuItem.setSelected(true);
			break;
		case 1:
			customMenuItem.setSelected(true);
			break;
		case 2:
			allMenuItem.setSelected(true);
			break;
		}
	}
}